class PredictiveSearch extends MenuDrawer{constructor(){super(),this.cachedResults={},this.input=this.querySelector('input[role="combobox"]'),this.predictiveSearchResults=this.querySelector("predictive-search-results"),this.setupEventListeners(),this.searchMenu=document.querySelector("[data-search-menu]"),this.searchMenuHTML=this.searchMenu.innerHTML}openMenuDrawer(summaryElement){super.openMenuDrawer(summaryElement),setTimeout(()=>{this.input.focus()},50)}setupEventListeners(){this.querySelector("form").addEventListener("submit",this.onFormSubmit.bind(this)),this.input.addEventListener("input",debounce(event=>{this.onChange(event)},300).bind(this)),this.input.addEventListener("focus",this.onFocus.bind(this)),this.addEventListener("focusout",this.onFocusOut.bind(this))}getQuery(){return this.input.value.trim()}hasQuery(){return!!this.getQuery().length}onChange(){const searchTerm=this.getQuery(),isDesktop=this.hasAttribute("data-desktop"),isEmptyInput=this.input.value==="";if(isDesktop&&isEmptyInput&&this.searchMenu){this.populateSearchResults(this.searchMenuHTML),this.close(!0);return}if(isEmptyInput){this.predictiveSearchResults.classList.add("hidden"),this.close(!0);return}if(this.predictiveSearchResults.classList.remove("hidden"),!this.hasQuery()){this.close(!0);return}this.getSearchResults(searchTerm)}onFormSubmit(event){(!this.hasQuery()||this.querySelector('[aria-selected="true"] a'))&&event.preventDefault()}onFocus(){const searchTerm=this.getQuery();this.hasQuery()&&(this.getAttribute("results")==="true"?this.open():this.getSearchResults(searchTerm))}onFocusOut(){setTimeout(()=>{this.contains(document.activeElement)||this.close()})}getSearchResults(searchTerm){const queryKey=searchTerm.replace(" ","-").toLowerCase();if(this.cachedResults[queryKey]){this.renderSearchResults(this.cachedResults[queryKey]);return}fetch(`${routes.predictive_search_url}?q=${encodeURIComponent(searchTerm)}&${encodeURIComponent("resources[type]")}=product,article,page&${encodeURIComponent("resources[limit]")}=5§ion_id=predictive-search`).then(response=>{if(!response.ok){const error=new Error(response.status);throw this.close(),error}return response.text()}).then(text=>{const resultsMarkup=new DOMParser().parseFromString(text,"text/html").querySelector("#shopify-section-predictive-search").innerHTML;this.cachedResults[queryKey]=resultsMarkup,this.renderSearchResults(resultsMarkup)}).catch(error=>{throw this.close(),error})}populateSearchResults(html=""){this.predictiveSearchResults.innerHTML=html}renderSearchResults(resultsMarkup){this.populateSearchResults(resultsMarkup),this.open()}open(){this.classList.remove("hidden"),this.setAttribute("open",!0),this.input.setAttribute("aria-expanded",!0)}close(clearSearchTerm=!1){clearSearchTerm&&(this.input.value="",this.removeAttribute("open"));const selected=this.querySelector('[aria-selected="true"]');selected&&selected.setAttribute("aria-selected",!1),this.input.setAttribute("aria-activedescendant",""),this.removeAttribute("open"),this.input.setAttribute("aria-expanded",!1)}}customElements.define("predictive-search",PredictiveSearch); //# sourceMappingURL=/cdn/shop/t/13/assets/predictive-search.js.map?v=140618324004693335231732871728